// eslint-disable-next-line import/named
import { ActionTree } from 'vuex'
import { StateInterface } from '../state'
import { WorkbenchStateInterface } from './state'

const actions: ActionTree<WorkbenchStateInterface, StateInterface> = {
  storeLoad ({ commit }) {
    const storeData = localStorage.getItem('WORKBENCH_EVENT')
    if (storeData) {
      const events = JSON.parse(storeData)
      commit('initEvents', events)
    }
  },
  storeSave ({ state }) {
    localStorage.setItem('WORKBENCH_EVENT', JSON.stringify(state.events))
  },
  emitEvent ({
    commit,
    dispatch
  }, event) {
    commit('pushEvent', event)
    dispatch('storeSave', null, { root: true })
  },
  async emitAppEvent ({ dispatch }, {
    app,
    operation
  }) {
    await dispatch('emitEvent', {
      title: '操作日志',
      type: 2,
      data: {
        operation: operation,
        name: app.appData.name,
        icon: app.appData.icon
      }
    })
  },
  updateOptions ({
    state,
    commit,
    dispatch
  }, options) {
    commit('setOptions', {
      ...state.options,
      ...options
    })
    dispatch('storeSave', null, { root: true })
  }
}

export default actions
